<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 多层的


        // 多层对象或者数组如何深拷贝
        var obj1 = {
            name : '张三',
            age : 20,
            hobby:{
                name1 : "篮球",
                name2 : "足球"
            }
        }
        function deepCopy(obj1){
            var obj2 = Object.prototype.toString.call(obj1) === "[object Array]" ? [] : {}
            for(var key in obj1){
                if(obj1.hasOwnProperty(key)){  // hasOwmProperty 判断obj1的key名是不是属于obj1这个对象
                    if(typeof obj1[key]=== "object"){
                    obj2[key] = deepCopy(obj1[key]);    // 每次循环调用此函数并赋值给
                }else{
                    obj2[key] = obj1[key];
                }
            }
        }
        return obj2;
    }
    var obj2 = deepCopy(obj1);
    console.log(obj2);
    obj1.hobby.name1 = '乒乓球'
    console.log(obj1);








    </script>
</body>
</html>